<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Boot 全局异常处理完全指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border: 1px solid rgba(102, 126, 234, 0.3);
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 12px;
            color: #888;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 8px;
        }
        
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            color: #d4d4d4;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 20px;
        }
        
        .section-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 32px;
            height: 32px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: 600;
            margin-right: 12px;
        }
        
        .advantage-tag {
            display: inline-block;
            padding: 4px 12px;
            background: rgba(102, 126, 234, 0.1);
            color: #667eea;
            border-radius: 20px;
            font-size: 14px;
            margin-right: 8px;
            margin-bottom: 8px;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white">
        <div class="container mx-auto px-6 py-20">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl font-bold mb-6 leading-tight">
                    Spring Boot 全局异常处理
                    <span class="block text-3xl mt-4 font-normal opacity-90">优雅管理应用异常的艺术</span>
                </h1>
                <p class="text-xl opacity-90 leading-relaxed">
                    掌握多种异常处理策略，构建健壮可靠的企业级应用
                </p>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>Spring Boot 中处理全局异常有多种方式，可以根据具体的需求选择合适的策略。对于大多数场景来说，<code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@ControllerAdvice</code> 或 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@RestControllerAdvice</code> 是最常用的方式，因为它们既方便又灵活，适合集中管理和处理应用中的异常。此外，通过合理配置和自定义异常处理逻辑，可以提高应用的健壮性和用户体验。
            </p>
        </div>

        <!-- Exception Handling Strategies Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-2xl font-bold mb-6 text-gray-800 text-center">异常处理策略概览</h2>
            <div class="mermaid">
                graph TD
                    A[Spring Boot 全局异常处理] --> B[@ControllerAdvice]
                    A --> C[@ResponseStatus]
                    A --> D[@RestControllerAdvice]
                    A --> E[HandlerExceptionResolver]
                    A --> F[ErrorController]
                    
                    B --> B1[集中处理]
                    B --> B2[灵活配置]
                    
                    C --> C1[简单直接]
                    C --> C2[状态码绑定]
                    
                    D --> D1[REST API专用]
                    D --> D2[JSON响应]
                    
                    E --> E1[完全自定义]
                    E --> E2[底层控制]
                    
                    F --> F1[统一错误页]
                    F --> F2[全局兜底]
                    
                    style A fill:#667eea,stroke:#764ba2,stroke-width:3px,color:#fff
                    style B fill:#e0e7ff,stroke:#667eea,stroke-width:2px
                    style C fill:#e0e7ff,stroke:#667eea,stroke-width:2px
                    style D fill:#e0e7ff,stroke:#667eea,stroke-width:2px
                    style E fill:#e0e7ff,stroke:#667eea,stroke-width:2px
                    style F fill:#e0e7ff,stroke:#667eea,stroke-width:2px
            </div>
        </div>

        <!-- Method 1: @ControllerAdvice -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h3 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="section-number">1</span>
                使用 @ControllerAdvice 和 @ExceptionHandler 注解
            </h3>
            
            <p class="text-gray-700 mb-6 leading-relaxed">
                <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@ControllerAdvice</code> 是 Spring 提供的一个注解，用于定义全局的异常处理器。你可以在这个类中使用 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@ExceptionHandler</code> 注解来处理特定的异常类型。
            </p>

            <div class="code-block mb-6">
                <div class="code-header">
                    <div class="code-dots">
                        <span class="dot dot-red"></span>
                        <span class="dot dot-yellow"></span>
                        <span class="dot dot-green"></span>
                    </div>
                    <span>GlobalExceptionHandler.java</span>
                </div>
                <pre><code>@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity&lt;String&gt; handleException(Exception e) {
        // 打印异常信息到日志
        e.printStackTrace();
        // 返回一个响应给客户端
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误");
    }

    @ExceptionHandler(NullPointerException.class)
    public ResponseEntity&lt;String&gt; handleNullPointerException(NullPointerException e) {
        e.printStackTrace();
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("空指针异常");
    }

    // 可以定义更多的异常处理方法...
}</code></pre>
            </div>

            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-xl p-6">
                <h4 class="font-bold text-gray-800 mb-3 flex items-center">
                    <i class="fas fa-star text-purple-600 mr-2"></i>
                    优点
                </h4>
                <div>
                    <span class="advantage-tag"><i class="fas fa-check-circle mr-1"></i>可以集中处理不同的异常，逻辑清晰</span>
                    <span class="advantage-tag"><i class="fas fa-check-circle mr-1"></i>通过 ResponseEntity 返回自定义的 HTTP 状态码和响应消息</span>
                </div>
            </div>
        </div>

        <!-- Method 2: @ResponseStatus -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h3 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="section-number">2</span>
                使用 @ResponseStatus 注解
            </h3>
            
            <p class="text-gray-700 mb-6 leading-relaxed">
                <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@ResponseStatus</code> 注解可以直接标注在自定义异常类上，指定 HTTP 状态码和响应消息。适用于简单的异常处理。
            </p>

            <div class="code-block mb-6">
                <div class="code-header">
                    <div class="code-dots">
                        <span class="dot dot-red"></span>
                        <span class="dot dot-yellow"></span>
                        <span class="dot dot-green"></span>
                    </div>
                    <span>ResourceNotFoundException.java</span>
                </div>
                <pre><code>@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {

    public ResourceNotFoundException(String message) {
        super(message);
    }
}</code></pre>
            </div>

            <p class="text-gray-700 mb-6">
                当 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">ResourceNotFoundException</code> 被抛出时，Spring Boot 会自动返回 404 状态码，并在响应体中包含异常信息。
            </p>

            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-xl p-6">
                <h4 class="font-bold text-gray-800 mb-3 flex items-center">
                    <i class="fas fa-star text-purple-600 mr-2"></i>
                    优点
                </h4>
                <div>
                    <span class="advantage-tag"><i class="fas fa-check-circle mr-1"></i>简单且与异常类紧密结合，适合单一异常处理</span>
                    <span class="advantage-tag"><i class="fas fa-check-circle mr-1"></i>不需要额外配置，全局异常处理更方便</span>
                </div>
            </div>
        </div>

        <!-- Method 3: @RestControllerAdvice -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h3 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="section-number">3</span>
                使用 @RestControllerAdvice 注解
            </h3>
            
            <p class="text-gray-700 mb-6 leading-relaxed">
                <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@RestControllerAdvice</code> 是 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@ControllerAdvice</code> 的一个变体，专门用于 RESTful API 的异常处理。它会自动在处理方法上加上 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">@ResponseBody</code>，将异常处理结果作为 JSON 返回给客户端。
            </p>

            <div class="code-block mb-6">
                <div class="code-header">
                    <div class="code-dots">
                        <span class="dot dot-re